package com.cloudptt.api.product.func.call.group.fsm.video;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.fsm.call.GroupCallAgent;
import com.cloudptt.api.product.func.call.group.fsm.floor.ISubState;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.AddrInfo;

public class VideoNoPermissionState implements ISubState
{
	private VideoAgent agent = null;
	private boolean isIdle = true;
	public VideoNoPermissionState(VideoAgent agent)
	{
		this.agent = agent;
		Log.d(GroupCallAgent.Log_Key, "currentState is VideoNoPermissionState");
	}

	public int doUserAction(int btnAction)
	{
		if (CallConst.BtnAction_Request == btnAction) 
		{
			if(isIdle)
			{
				int result = agent.TransRequest();
				if(CauseCont.OperateCheck_Suc == result)
				{
					agent.goToState(CallConst.VideoState_PendingRequest);
				}
				return result;
			}
		} 
		else if (CallConst.BtnAction_Release == btnAction) 
		{
		}
		return CauseCont.OperateErr_Exception;
	}
	@Override
	public void receiveRtcpMsg(RtcpMsg msg) {
		// TODO Auto-generated method stub
		Log.d(GroupCallAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg msg = " + msg);
		if(msg == null)
		{
			Log.d(GroupCallAgent.Log_Key, "currentState is IdleState msg == null");
			return;
		}
		String title = msg.getTitle();
		Log.d(GroupCallAgent.Log_Key, "receiveRtcpMsg title = " + title);
		if(title.equals(RtcpMsg.Title_MCV1_MEDIA_NOTE))
		{
			if(isIdle)
			{
				String num = msg.getValue(RtcpMsg.Key_From);
				Log.d(GroupCallAgent.Log_Key, "receiveRtcpMsg num = " + num);
				this.agent.updateVideoPermissionNum(num);
			}

			isIdle = false;
		}
		else if(title.equals(RtcpMsg.Title_MCV1_IDLE))
		{
			if(!isIdle)
			{
				this.agent.updateVideoPermissionNum("");
			}
			isIdle = true;
			
		}
		else if(title.equals(RtcpMsg.Title_MCV1_GRANTED))
		{
			agent.goToState(CallConst.VideoState_Permission);
		}
	}
	public void updateTakenUser(String callType,AddrInfo gInfo,AddrInfo user)
	{}
	public void destory()
	{
		agent = null;
	}

	@Override
	public int getState() {
		// TODO Auto-generated method stub
		return CallConst.VideoState_NoPermission;
	}

	
}
